<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>

    </style>
</head>
<body>
    cookie的封装
</body>
<script>
    setCookie("a",10);
    setCookie("b",20,{});
    setCookie("c",30,{
        path:"/abc"
    });
    setCookie("d",40,{
        expires: 3
    });
    setCookie("e",50,{
        expires: 7,
        path:"/abc"
    });

    // 设置cookie封装
    function setCookie(key, val, ops={}){
        // 根据参数处理有效期部分的字符
        let e = "";
        if(ops.expires){
            const d = new Date();
            d.setDate(d.getDate() + ops.expires)
            e = ";expires="+d;
        }
        // 根据参数处理路径部分的字符
        let p = "";
        if(ops.path){
            p = ";path="+ops.path
        }
        // 使用字符串拼接，设置cookie
        document.cookie = `${key}=${val}${e}${p}`;
    }
</script>
</html>